//
//  SPHTTPRequestOperationManager.m
//  SuperPartner
//
//  Created by EvanZ on 15-3-15.
//  Copyright (c) 2015年 EvanZ. All rights reserved.
//

#import "SPHTTPRequestOperationManager.h"

@implementation SPHTTPRequestOperationManager

- (void)uploadPhotoParameters:(NSDictionary *)parameters
                           success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
                      failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
                     progress:(void (^)(float percent))progressBlock
{
    self.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
    NSString * urlStr = [[NSString stringWithFormat:@"%@/keyway-api-1/uploadphoto",BASED_URL] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSString *path = parameters[@"filepath"];
    NSURL *fileUrl = [NSURL fileURLWithPath:path];
    NSString *mimeType = @"image/jpeg";
    
    NSMutableURLRequest *request = [self.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:urlStr parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileURL:fileUrl name:@"photo" fileName:path mimeType:mimeType error:nil];
        [formData appendPartWithFormData:[@"ivanz1234@163.com" dataUsingEncoding:NSUTF8StringEncoding] name:@"userid"];
    } error:nil];
    
//    [request setValue:@"application/octet-stream; boundary=---------------------------123821742118716" forHTTPHeaderField:@"Content-Type"];
//    [request setValue:@"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; QQDownload 534; TencentTraveler 4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; CIBA; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; InfoPath.2)" forHTTPHeaderField:@"User-Agent"];
//    [request setValue:@"Keep-Alive" forHTTPHeaderField:@"Connection"];
    NSLog(@"Headers: %@", [request allHTTPHeaderFields]);
    
    AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
    [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
        progressBlock((float)totalBytesWritten / (float)totalBytesExpectedToWrite);
    }];
    operation.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    self.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
    
    [self.operationQueue addOperation:operation];
}

//login
- (void)loginPara:(NSDictionary*)para Success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
          failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure{
    NSString * urlStr = [[NSString stringWithFormat:@"%@/partner/Home/login",BASED_URL] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"\n login \n POST url:%@ \npara:%@",urlStr,para);
    [self POST:urlStr parameters:para success:success failure:failure];
}

//sign in
- (void)signinPara:(NSDictionary*)para Success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
           failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure{
    NSString * urlStr = [[NSString stringWithFormat:@"%@/partner/Home/user",BASED_URL] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"\n sign_in \n POST url:%@ \npara:%@",urlStr,para);
    [self POST:urlStr parameters:para success:success failure:failure];

}

//get user info
- (void)getUserInfoWithID:(NSString*)pid Success:(void (^)(AFHTTPRequestOperation *, id))success failure:(void (^)             (AFHTTPRequestOperation *, NSError *))failure{
    
    NSString * urlStr = [[NSString stringWithFormat:@"%@/partner/Home/user?user_id=%@",BASED_URL,pid] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"\n get_user_info \n GET url:%@",urlStr);
    [self GET:urlStr parameters:nil success:success failure:failure];
}

//get home info
- (void)getHomeInfoPara:(NSDictionary*)para Success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
                      failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure{
    NSString * urlStr = [[NSString stringWithFormat:@"%@/partner/Home/",BASED_URL] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"\n get_home_info \n POST url:%@ \npara:%@",urlStr,para);
    [self POST:urlStr parameters:para success:success failure:failure];
}

//get partners
- (void)getPartnersInfoWithID:(NSString*)pid Success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
                       failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure{
    NSString * urlStr = [[NSString stringWithFormat:@"%@/partner/Home/partners?user_id=%@",BASED_URL,pid] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"\n get_partners_info \n GET url:%@",urlStr);
    [self GET:urlStr parameters:nil success:success failure:failure];
}

//set invitation code
- (void)setInvitationCodePara:(NSDictionary*)para Success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
                      failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure{
    NSString * urlStr = [[NSString stringWithFormat:@"%@/partner/Home/invitation",BASED_URL] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"\n set_invatation_code \n POST url:%@ \npara:%@",urlStr,para);
    [self POST:urlStr parameters:para success:success failure:failure];
}

//send verification code to cell phone
- (void)sendVerificationCodePara:(NSDictionary*)para Success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
                         failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure{
    NSString * urlStr = [[NSString stringWithFormat:@"%@/partner/Home/phoneCode",BASED_URL] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"\n send_verification_code \n POST url:%@ \npara:%@",urlStr,para);
    [self POST:urlStr parameters:para success:success failure:failure];
}

//send signin verfication code
- (void)sendSigninVerificationCodePara:(NSDictionary*)para Success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
                               failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure{
    NSString * urlStr = [[NSString stringWithFormat:@"%@/partner/Home/user/phoneCode",BASED_URL] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"\n send_verification_code \n POST url:%@ \npara:%@",urlStr,para);
    [self POST:urlStr parameters:para success:success failure:failure];
}

//reset password
- (void)resetPasswordPara:(NSDictionary*)para Success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
                  failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure{
    NSString * urlStr = [[NSString stringWithFormat:@"%@/partner/Home/changePwd",BASED_URL] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"\n reset_password \n POST url:%@ \npara:%@:",urlStr,para);
    [self POST:urlStr parameters:para success:success failure:failure];
}

@end